<?php
declare (strict_types = 1);

namespace app\middleware;

use app\util\ReturnCode;

class AdminAuth
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $apiAuth = $request->header('apiAuth','');
        if ($apiAuth){
            $userInfo = cache('Login:'.$apiAuth);
            if ($userInfo!==null){
                $userInfo = json_decode($userInfo,true);
            }
            if (!$userInfo || !isset($userInfo['id'])){
                return json([
                    'code' =>ReturnCode::AUTH_ERROR,
                    'msg' =>'ApiAuth不匹配',
                    'data' =>[]
                ]);
            }else{
                $request->API_ADMIN_USER_INFO = $userInfo;
            }
            return $next($request);
        }else{
            return json([
                'code'=>ReturnCode::AUTH_ERROR,
                'msg'=>'缺少apiAuth',
                'data'=>[]
            ]);
        }
    }
}
